<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Tag_Services_Tag
{	
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
		
		$data['messages']['tag_text'] = $validator->setString($data['tag_text'])
												->validate(array('notNull')
																, array(
																	'notNull' => 'tag_text_notnull',
																)
														);
		
		$data['messages']['slug'] = $validator->setString($data['slug'])
												->validate(array('notNull', 'isSlug')
																, array(
																	'notNull' => 'tag_slug_notnull',
																	'isSlug' => 'tag_slug_invalid',
																)
														);
		
		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function add($tag)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->add($tag);	
	}
	
	public static function update($tag)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->update($tag);	
	}
	
	public static function delete($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->delete($id);	
	}
		
	public static function checkExist($text, $tagId = null)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->checkExist($text, $tagId);	
	}
	
	public static function getById($id)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->getById($id);	
	}
	
	public static function find($start, $limit, $condition = array())
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->find($start, $limit, $condition);
	}
	
	public static function count($condition)
	{
		$conn	= Gio_Db_Connection::getConnection();
		$dao 	= new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->count($condition);
	}
	
	public static function getByItem($item)
	{
		$conn		= Gio_Db_Connection::getConnection();
		$dao = new Modules_Tag_Models_Mysql_Tag();
		$dao->setConnection($conn);
		return $dao->getByItem($item);
	}
}